<?php

header("Content-type:application/json; charset=UTF-8");          
header("Cache-Control: no-store, no-cache, must-revalidate");         
header("Cache-Control: post-check=0, pre-check=0", false);  

require('../service/login_service.php');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$wsname = $_REQUEST['wsname'];
$tokeyKey = $_REQUEST['token'];

class login_ws
{
    public function authentication($username, $password)
    {
        $login_service = new login_service();
        
        $lists = $login_service->findUserByUsername($username);
//         echo "PASS: ".$lists->getPassword();
//         echo "MD5: ".crypt($password, $lists->getPassword());
        
        if(crypt($password, $lists->getPassword()) == $lists->getPassword()) {
            $result = array(
                        "user" => $lists,
                        "isauthen" => TRUE
                      );
        } else {
            $result = array(
                        "user" => NULL,
                        "isauthen" => FALSE
                      );
        }
        
        return apputil::getJsonDataResultUtil($result, 'authentication');
    }
    
    public function isValidateToken($tokeyKey)
    {
        $login_service = new login_service();
        return $login_service->isValidateTokenKey($tokeyKey);
    }

}

//init class
$login_ws = new login_ws();

if ($login_ws->isValidateToken($tokeyKey)) {
    if ($wsname == 'authen') {
        $username = isset($_REQUEST['username']) ? $_REQUEST['username'] : null;
        $password = isset($_REQUEST['password']) ? $_REQUEST['password'] : null;
        print_r($login_ws->authentication($username, $password));
    } else {
        echo 'web service error !!';
    }
} else {
    echo 'Your token is incorrect !!';
}
?>